﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MensUnoShop.Models;

namespace MensUnoShop.Models.Discount
{
    /// <summary>
    /// 固定判斷之購物金折抵
    /// </summary>
    [Serializable]
    public class BirthdayDiscount:ITradeInDiscount
    {
        private string _title;
        private int _deprice;
        private int _totalprice;
        private DateTime _birth,_datein;
        private Order _od;
        private Config.OrderDiscountType _sktype;
        public string TITLE { set { _title = value; } get { return _title; } }
        public int DEPRICE { set { _deprice = value; } get { return _deprice; } }
        public Config.OrderDiscountType SKTYPE { set { _sktype = value; } get { return _sktype; } }


        public BirthdayDiscount()
        {
            _title = "生日折扣 Birthday Discount";
            _deprice = 0;
            _sktype = Config.OrderDiscountType.BirthDiscount;
        }


        public BirthdayDiscount(DateTime birth,DateTime datein, Order od)
            : this()
        {
            //扣掉行銷機制後的金額
            _totalprice = od.TOTALPRICE-od.DSKLIST.Sum(d=>d.DEPRICE);
            _birth = birth;
            _datein = datein;
            _od = od;
        }

        public void GetDiscount()
        {
            if (!_od.MLIST.Any(m => m.TYPE == Config.MarketType.Mangazine))
            {
                SenerPro.Logger log = new SenerPro.Logger(Config.logpath);
                DBSaleskit dbs = new DBSaleskit();
                bool isused = dbs.GetBirthdayDiscount(_od.MGUID);
                if (!isused && _birth.Date.Month == DateTime.Today.Month && _totalprice >= 3000 && (DateTime.Today.Date - _datein.Date).Days > 30)
                {
                    _deprice = (int)((double)_totalprice * (double)(0.1));
                }
            }
            else
                _deprice = 0;
        }

        public bool SetDiscount(string txid)
        {
            DBSaleskit dbs = new DBSaleskit();
            bool isset = dbs.SetBirthdayDiscount(_od.MGUID, _od.OGUID);
            return isset;
        }

        /// <summary>
        /// 交易失敗處理
        /// </summary>
        public void DealFail(string txid)
        {
            DBSaleskit dbs = new DBSaleskit();
            dbs.DelBirthdayDiscount(txid);
        }
    }
}
